package edu.uoc.tdp.pac4.client.maintenance;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.HeadlessException;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import edu.uoc.tdp.pac4.beans.Rol;
import edu.uoc.tdp.pac4.beans.Sex;
import edu.uoc.tdp.pac4.beans.Usuari;
import edu.uoc.tdp.pac4.client.ClientConnectionHelper;
import edu.uoc.tdp.pac4.common.MantenimentInterface;
import edu.uoc.tdp.pac4.common.util.LanguageUtils;

public class UserCreateWindow extends JFrame {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -2659672914419974455L;
	private ClientConnectionHelper connectionHelper_;
	private MantenimentInterface maintenance;
	
	private JTextField txtRegister;
	private JTextField txtLogin;
	private JTextField txtPassword;
	private JTextField txtName;
	private JTextField txtSurname;
	private JTextField txtNif;
	private JTextField txtBirthday;
	private JTextField txtEmail;
	private JComboBox<Rol> cbRol;
	private ButtonGroup groupSex = new ButtonGroup();
	private ButtonGroup groupStatus = new ButtonGroup();

	SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yy");
	private Usuari usuari;
	
	public UserCreateWindow(ClientConnectionHelper connectionHelper_)
			throws HeadlessException {
		this(null, connectionHelper_);
	}
	
	/**
	 * @wbp.parser.constructor
	 */
	public UserCreateWindow(final Usuari usuari,
			ClientConnectionHelper connectionHelper_) throws HeadlessException {
		super();
		setTitle("User create");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 358);
		
		this.usuari = usuari;
		this.connectionHelper_ = connectionHelper_;
		this.maintenance=connectionHelper_.getMaintenanceManager();

		JPanel panel_1 = new JPanel();
		
		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.CENTER);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[]{147, 35, 114, 0};
		gbl_panel.rowHeights = new int[]{0, 19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
		gbl_panel.columnWeights = new double[]{0.0, 1.0, 1.0, Double.MIN_VALUE};
		gbl_panel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		panel.setLayout(gbl_panel);
		
		//register
		JLabel lblRegister = new JLabel(LanguageUtils.getMessage("client.maintenance.usermanag.labelInactivityDate"));
		GridBagConstraints gbc_lblRegister = new GridBagConstraints();
		gbc_lblRegister.insets = new Insets(0, 0, 5, 5);
		gbc_lblRegister.anchor = GridBagConstraints.WEST;
		gbc_lblRegister.gridx = 0;
		gbc_lblRegister.gridy = 0;
		panel.add(lblRegister, gbc_lblRegister);
		
		txtRegister = new JTextField();
		txtRegister.setEditable(false);
		//txtRegister.setText("register");
		GridBagConstraints gbc_txtRegister = new GridBagConstraints();
		gbc_txtRegister.insets = new Insets(0, 0, 5, 5);
		gbc_txtRegister.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtRegister.gridx = 1;
		gbc_txtRegister.gridy = 0;
		panel.add(txtRegister, gbc_txtRegister);
		txtRegister.setColumns(10);

		//login
		JLabel lblLogin = new JLabel(LanguageUtils.getMessage("client.maintenance.usermanag.labelLogin"));
		GridBagConstraints gbc_lblLogin = new GridBagConstraints();
		gbc_lblLogin.insets = new Insets(0, 0, 5, 5);
		gbc_lblLogin.anchor = GridBagConstraints.WEST;
		gbc_lblLogin.gridx = 0;
		gbc_lblLogin.gridy = 1;
		panel.add(lblLogin, gbc_lblLogin);
		
		txtLogin = new JTextField();
		//txtLogin.setText("login");
		GridBagConstraints gbc_txtLogin = new GridBagConstraints();
		gbc_txtLogin.insets = new Insets(0, 0, 5, 5);
		gbc_txtLogin.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtLogin.gridx = 1;
		gbc_txtLogin.gridy = 1;
		panel.add(txtLogin, gbc_txtLogin);
		txtLogin.setColumns(10);
		
		//password
		JLabel lblPassword = new JLabel(LanguageUtils.getMessage("client.maintenance.usermanag.labelPassword"));
		GridBagConstraints gbc_lblPassword = new GridBagConstraints();
		gbc_lblPassword.anchor = GridBagConstraints.WEST;
		gbc_lblPassword.insets = new Insets(0, 0, 5, 5);
		gbc_lblPassword.gridx = 0;
		gbc_lblPassword.gridy = 2;
		panel.add(lblPassword, gbc_lblPassword);
		
		txtPassword = new JTextField();
		//txtPassword.setText("password");
		GridBagConstraints gbc_txtPassword = new GridBagConstraints();
		gbc_txtPassword.insets = new Insets(0, 0, 5, 5);
		gbc_txtPassword.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtPassword.gridx = 1;
		gbc_txtPassword.gridy = 2;
		panel.add(txtPassword, gbc_txtPassword);
		txtPassword.setColumns(10);
		
		//name
		JLabel lblName = new JLabel(LanguageUtils.getMessage("client.maintenance.usermanag.labelName"));
		GridBagConstraints gbc_lblName = new GridBagConstraints();
		gbc_lblName.insets = new Insets(0, 0, 5, 5);
		gbc_lblName.anchor = GridBagConstraints.WEST;
		gbc_lblName.gridx = 0;
		gbc_lblName.gridy = 3;
		panel.add(lblName, gbc_lblName);
		
		txtName = new JTextField();
		//txtName.setText("name");
		GridBagConstraints gbc_txtName = new GridBagConstraints();
		gbc_txtName.insets = new Insets(0, 0, 5, 5);
		gbc_txtName.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtName.gridx = 1;
		gbc_txtName.gridy = 3;
		panel.add(txtName, gbc_txtName);
		txtName.setColumns(10);
		
		//surname
		JLabel lblSurname = new JLabel(LanguageUtils.getMessage("client.maintenance.usermanag.labelSurnames"));
		GridBagConstraints gbc_lblSurname = new GridBagConstraints();
		gbc_lblSurname.insets = new Insets(0, 0, 5, 5);
		gbc_lblSurname.anchor = GridBagConstraints.WEST;
		gbc_lblSurname.gridx = 0;
		gbc_lblSurname.gridy = 4;
		panel.add(lblSurname, gbc_lblSurname);
		
		txtSurname = new JTextField();
		//txtSurname.setText("surname");
		GridBagConstraints gbc_txtSurname = new GridBagConstraints();
		gbc_txtSurname.insets = new Insets(0, 0, 5, 5);
		gbc_txtSurname.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtSurname.gridx = 1;
		gbc_txtSurname.gridy = 4;
		panel.add(txtSurname, gbc_txtSurname);
		txtSurname.setColumns(10);
		
		//nif
		JLabel lblNif = new JLabel(LanguageUtils.getMessage("client.maintenance.usermanag.labelNIF"));
		GridBagConstraints gbc_lblNif = new GridBagConstraints();
		gbc_lblNif.insets = new Insets(0, 0, 5, 5);
		gbc_lblNif.anchor = GridBagConstraints.WEST;
		gbc_lblNif.gridx = 0;
		gbc_lblNif.gridy = 5;
		panel.add(lblNif, gbc_lblNif);
		
		txtNif = new JTextField();
		//txtNif.setText("nif");
		GridBagConstraints gbc_txtNif = new GridBagConstraints();
		gbc_txtNif.insets = new Insets(0, 0, 5, 5);
		gbc_txtNif.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtNif.gridx = 1;
		gbc_txtNif.gridy = 5;
		panel.add(txtNif, gbc_txtNif);
		txtNif.setColumns(10);
		
		//birthday
		JLabel lblBirthday = new JLabel(LanguageUtils.getMessage("client.maintenance.usermanag.labelBirthday"));
		GridBagConstraints gbc_lblBirthday = new GridBagConstraints();
		gbc_lblBirthday.insets = new Insets(0, 0, 5, 5);
		gbc_lblBirthday.anchor = GridBagConstraints.WEST;
		gbc_lblBirthday.gridx = 0;
		gbc_lblBirthday.gridy = 6;
		panel.add(lblBirthday, gbc_lblBirthday);
		
		txtBirthday = new JTextField();
		//txtBirthday.setText("birthday");
		GridBagConstraints gbc_txtBirthday = new GridBagConstraints();
		gbc_txtBirthday.insets = new Insets(0, 0, 5, 5);
		gbc_txtBirthday.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtBirthday.gridx = 1;
		gbc_txtBirthday.gridy = 6;
		panel.add(txtBirthday, gbc_txtBirthday);
		txtBirthday.setColumns(10);
		
		//email
		JLabel lblEmail = new JLabel(LanguageUtils.getMessage("client.maintenance.usermanag.labelEmail"));
		GridBagConstraints gbc_lblEmail = new GridBagConstraints();
		gbc_lblEmail.insets = new Insets(0, 0, 5, 5);
		gbc_lblEmail.anchor = GridBagConstraints.WEST;
		gbc_lblEmail.gridx = 0;
		gbc_lblEmail.gridy = 7;
		panel.add(lblEmail, gbc_lblEmail);
		
		txtEmail = new JTextField();
		//txtEmail.setText("email");
		GridBagConstraints gbc_txtEmail = new GridBagConstraints();
		gbc_txtEmail.insets = new Insets(0, 0, 5, 5);
		gbc_txtEmail.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtEmail.gridx = 1;
		gbc_txtEmail.gridy = 7;
		panel.add(txtEmail, gbc_txtEmail);
		txtEmail.setColumns(10);
		
		//rol
		JLabel lblRol = new JLabel(LanguageUtils.getMessage("client.maintenance.usermanag.labelRol"));
		GridBagConstraints gbc_lblRol = new GridBagConstraints();
		gbc_lblRol.insets = new Insets(0, 0, 5, 5);
		gbc_lblRol.anchor = GridBagConstraints.WEST;
		gbc_lblRol.gridx = 0;
		gbc_lblRol.gridy = 8;
		panel.add(lblRol, gbc_lblRol);
		
		cbRol = new JComboBox(Rol.values());
		GridBagConstraints gbc_cbRol = new GridBagConstraints();
		gbc_cbRol.insets = new Insets(0, 0, 5, 5);
		gbc_cbRol.fill = GridBagConstraints.HORIZONTAL;
		gbc_cbRol.gridx = 1;
		gbc_cbRol.gridy = 8;
		panel.add(cbRol, gbc_cbRol);
		
		
		//sex
		JLabel lblSex = new JLabel(LanguageUtils.getMessage("client.maintenance.usermanag.labelSex"));
		GridBagConstraints gbc_lblSex = new GridBagConstraints();
		gbc_lblSex.anchor = GridBagConstraints.WEST;
		gbc_lblSex.insets = new Insets(0, 0, 5, 5);
		gbc_lblSex.gridx = 0;
		gbc_lblSex.gridy = 9;
		panel.add(lblSex, gbc_lblSex);

		JRadioButton rdbtnMale = new JRadioButton(Sex.Home.name());
		GridBagConstraints gbc_rdbtnMale = new GridBagConstraints();
		gbc_rdbtnMale.anchor = GridBagConstraints.WEST;
		gbc_rdbtnMale.insets = new Insets(0, 0, 5, 5);
		gbc_rdbtnMale.gridx = 1;
		gbc_rdbtnMale.gridy = 9;
		panel.add(rdbtnMale, gbc_rdbtnMale);
		
		JRadioButton rdbtnFemale = new JRadioButton(Sex.Dona.name());
		GridBagConstraints gbc_rdbtnFemale = new GridBagConstraints();
		gbc_rdbtnFemale.anchor = GridBagConstraints.WEST;
		gbc_rdbtnFemale.insets = new Insets(0, 0, 5, 0);
		gbc_rdbtnFemale.gridx = 2;
		gbc_rdbtnFemale.gridy = 9;
		panel.add(rdbtnFemale, gbc_rdbtnFemale);
		
		groupSex.add(rdbtnMale);
		groupSex.add(rdbtnFemale);
		
		//status
		JLabel lblStatus = new JLabel(LanguageUtils.getMessage("client.maintenance.usermanag.labelStatus"));
		GridBagConstraints gbc_lblStatus = new GridBagConstraints();
		gbc_lblStatus.anchor = GridBagConstraints.WEST;
		gbc_lblStatus.insets = new Insets(0, 0, 0, 5);
		gbc_lblStatus.gridx = 0;
		gbc_lblStatus.gridy = 10;
		panel.add(lblStatus, gbc_lblStatus);
		
		JRadioButton rdbtnActive = new JRadioButton("active");
		GridBagConstraints gbc_rdbtnActive = new GridBagConstraints();
		gbc_rdbtnActive.anchor = GridBagConstraints.WEST;
		gbc_rdbtnActive.insets = new Insets(0, 0, 0, 5);
		gbc_rdbtnActive.gridx = 1;
		gbc_rdbtnActive.gridy = 10;
		panel.add(rdbtnActive, gbc_rdbtnActive);
		
		JRadioButton rdbtnInactive = new JRadioButton("inactive");
		GridBagConstraints gbc_rdbtnInactive = new GridBagConstraints();
		gbc_rdbtnInactive.anchor = GridBagConstraints.WEST;
		gbc_rdbtnInactive.gridx = 2;
		gbc_rdbtnInactive.gridy = 10;
		panel.add(rdbtnInactive, gbc_rdbtnInactive);
		getContentPane().add(panel_1, BorderLayout.SOUTH);
		
		groupStatus.add(rdbtnActive);
		groupStatus.add(rdbtnInactive);
		
		
		
		JButton btnAccept = new JButton(LanguageUtils.getMessage("client.maintenance.usermanag.btnAccept"));
		btnAccept.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					Usuari usuariX= new Usuari();
					
					usuariX.setNombre(txtName.getText());
					usuariX.setApellidos(txtSurname.getText());		
					usuariX.setNacimiento(dateFormat.parse(txtBirthday.getText()));
					usuariX.setEmail(txtEmail.getText());
					usuariX.setSexe(Sex.valueOf(getSelection(groupSex).getText()));
					usuariX.setRol((Rol)cbRol.getSelectedItem());
					usuariX.setLogin(txtLogin.getText());
					usuariX.setContrasenya(txtPassword.getText());
					
					if(usuari == null){
						//usuariX.setDataBaixa();
						createUser(usuariX);					
					} else {
						usuariX.setNIF(usuari.getNIF());
						if("active".equals(getSelection(groupStatus).getText())){
							usuariX.setDataBaixa(null);
						} else{
							if(usuari.getDataBaixa() == null){
								usuariX.setDataBaixa(new Date(System.currentTimeMillis()));							
							}else {
								usuariX.setDataBaixa(usuari.getDataBaixa());					
							}			
						}
						updateUser(usuariX);
					}
					dispose();
				} catch (IllegalArgumentException ex) {
					JOptionPane.showMessageDialog(null, ex.getMessage(), null,
							JOptionPane.INFORMATION_MESSAGE);			
				} catch (RemoteException e1) {
					//LanguageUtils.getMessage("client.management.button.group.saved")
					e1.printStackTrace();
				} catch (ParseException e1) {
					JOptionPane.showMessageDialog(null, e1.getMessage(), null,
							JOptionPane.INFORMATION_MESSAGE);			
				}
				
			}

		});
		btnAccept.setHorizontalAlignment(SwingConstants.LEFT);
		panel_1.add(btnAccept);
		
		JButton btnCancel = new JButton(LanguageUtils.getMessage("client.maintenance.usermanag.btnCancel"));
		btnCancel.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		btnCancel.setHorizontalAlignment(SwingConstants.RIGHT);
		panel_1.add(btnCancel);
		
		if(usuari!=null) {
			if(usuari.getDataBaixa()!= null)
				txtRegister.setText(dateFormat.format(usuari.getDataBaixa()));
			txtSurname.setText(usuari.getApellidos());
			txtPassword.setText(usuari.getContrasenya());
			txtBirthday.setText(dateFormat.format(usuari.getNacimiento()));
			txtEmail.setText(usuari.getEmail());
			txtLogin.setText(usuari.getLogin());
			txtNif.setText(usuari.getNIF());
			txtName.setText(usuari.getNombre());

			cbRol.setSelectedItem(usuari.getRol());
			
			if(Sex.Home.equals(usuari.getSexe())){
				rdbtnMale.setSelected(true);				
			} else {
				rdbtnFemale.setSelected(true);								
			}
			
			if(usuari.isActiu()){
				rdbtnActive.setSelected(true);				
			} else {
				rdbtnInactive.setSelected(true);		
			}

			
		}
	}
	
	

	private Usuari createUser(Usuari usuariX) throws RemoteException, ParseException {
		Usuari createdUser = maintenance.createUser(usuariX);
		return usuari;
	}
	private Usuari updateUser(Usuari usuariX) throws RemoteException, ParseException {
		
		
		maintenance.updateUser(usuariX);
		
		return usuari;
	}

	  public static JRadioButton getSelection(ButtonGroup group) {
		for (Enumeration e = group.getElements(); e.hasMoreElements();) {
			JRadioButton b = (JRadioButton) e.nextElement();
			if (b.getModel() == group.getSelection()) {
				return b;
			}
		}
		return null;
	  }
	
	public static void main(String[] args) {
		
		UserCreateWindow userCreateWindow = new UserCreateWindow( new ClientConnectionHelper());
		userCreateWindow.setVisible(true);

	}

}
